import { exists } from "https://deno.land/std@0.97.0/fs/mod.ts"; import { build, stop } from 'https://deno.land/x/esbuild@v0.12.6/mod.js'; import type { BuildOptions, BuildResult } from 'https://deno.land/x/esbuild@v0.12.6/mod.js'; import httpFetch from 'https://deno.land/x/esbuild_plugin_http_fetch@v1.0.2/index.js' // 特定のpropertyを削るやつ type Omit = Pick>; export async function run(filename: string, options: Omit) { let useTempFile = false; let result: BuildResult | undefined = undefined; try { if (/^https?:\/\//.test(filename)) { const tempname = `index-${Math.random()}.css`; await Deno.writeTextFile(tempname, `@import '${filename}';`); filename = tempname; useTempFile = true; } const _options: BuildOptions = { entryPoints: [filename], platform: 'neutral', plugins: [httpFetch], ...options, }; result = await build(_options); stop(); } catch(e) { throw e; }finally { //後始末 if (useTempFile) await Deno.remove(filename); if (await exists('./cache')) await Deno.remove('./cache', { recursive: true }); } return result; }